﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Zhuang.Utility.Common.ConfigFile;
using Zhuang.Utility.Common.Conversion;
using Zhuang.Utility.Common.Encryption;
using System.Collections;
using Zhuang.Utility.Common.DataBase;
using Zhuang.WinForm.AppCode.Common;
using Zhuang.Utility.Common.Logs;
using Zhuang.Utility.WinForm.Forms;
using System.Reflection;
using Zhuang.Model.Access;
using Zhuang.Utility.Common.WinOS;
using Zhuang.WinForm.ControlLibrary.Components.Forms;
namespace Zhuang.WinForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            string msg = "";
            //MessageBox.Show(AppCode.SystemUtility.serverName + "-" + AppCode.SystemUtility.databaseName + "-" + AppCode.SystemUtility.databasePort + "-" + AppCode.SystemUtility.dbUserName + "-"+AppCode.SystemUtility.dbUserPassword);
            
            //INIFile ini = new INIFile(@"C:\a.ini");
            //ini.IniWriteValue("s1","b","都有付出");
            //ini.IniWriteValue("s2", "a", "2");
            //ini.IniWriteValue("s1", "name", "qwerqwer");

//            string msg = AES.Encode(@"2.N-VAR（宗教、信仰等的）改变，皈依，归附If someone changes their religion or beliefs, you can refer to their conversion to their new religion or beliefs. 
//            ...his conversion to Christianity...
//
//            他皈依基督教
//
//            It's hard to trust the President's conversion.
//
//            总统信仰的改变让人难以相信。
//
//            ", "268");
//            msg=AES.Decode(msg,"268");


            //msg = Zhuang.Utility.Common.DateAndTime.DateTimeHelper.FormatDate(DateTime.Now,textBox1.Text.Trim());

            //Zhuang.Utility.Common.Image.ImageThumbnailHelper.MakeThumbnail(@"C:\temp\01\img8.jpg", @"C:\temp\02\img8.jpg", 100, 100,Zhuang.Utility.Common.Image.ImageThumbnailHelper.ThumbnailMod.Cut);

            //msg = Zhuang.Utility.Common.Conversion.StringHelper.BarCode("201231234123",20,1,1);

            //Zhuang.Utility.Common.Print.ClsPrintLPT pt = new Zhuang.Utility.Common.Print.ClsPrintLPT();
            //pt.PrintPort = @"c:\a.txt";
            //ArrayList al=new ArrayList();
            //al.Add("adshoasdhfoashdfoashfisdhfoahofai");
            //al.Add("b");
            //al.Add("c");mberDAL();
            //msg = mnDal.GetMaxNumber(textBox1.Text.
            //al.Add("d");
            //al.Add("e");
            //pt.PrintArrayList(al,1);

            //Sys_MaxNumberDAL mnDal = new Sys_MaxNuTrim()) ;

            //OleDbHelper.ExecuteSql("insert into Erp_product(ProductId,ProductName) values('001','aaa')");


            //LogTextHelper.WriteLine("aaaaaaaaaaaaaaaaaaaaa");
            //LogTextHelper.WriteLine("Form1", "button1_Click", "aaaaa");

            ProductEntity pe = new ProductEntity();

            pe.ProductCode="aa";
            pe.ProductName="aaa";
            pe.RetailPrice=1.1m;

            msg = GetInsertSql(pe);

            MessageBox.Show(msg);
            
        }

        public string GetInsertSql(ProductEntity entity)
        {
            Type type = typeof(ProductEntity);
            PropertyInfo[] properties = type.GetProperties();

            string tbName = type.Name.Replace("Entity", ""); ;
            string strSql = "Insert into " + tbName + "(";
            string idName = "";
            foreach (PropertyInfo pi in properties)
            {
                if (pi.Name.Trim().ToLower() == (tbName + "Id").ToLower())
                {
                    idName = pi.Name;
                    continue;
                }

                strSql = strSql + pi.Name + ",";
            }

            strSql = strSql.Substring(0, strSql.Length - 1);

            strSql = strSql + ") values(";


            foreach (PropertyInfo pi in properties)
            {
                if (pi.Name.Trim().ToLower() == (tbName + "Id").ToLower())
                {
                    idName = pi.Name;
                    continue;
                }

                strSql = strSql + "'" + ReflectionHelper.GetProperty(entity, pi.Name) + "',";
            }
            strSql = strSql.Substring(0, strSql.Length - 1);

            strSql = strSql + ")";


            return strSql;

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            skinEngine1.SkinFile = Application.StartupPath + @"\Skin.ssk";
        }

        private void textBox1_DoubleClick(object sender, EventArgs e)
        {

            SelectUser();
            
        }

        private void SelectUser()
        {
            string strSql = string.Format("select * from sys_user where 1=1 and UserName Like '%{0}%' or Name like '%{1}%'", textBox1.Text.Trim(), textBox1.Text.Trim());

            //DataTable dt = SqlHelper.Query(strSql).Tables[0] ;

            SearchSelect ss = new SearchSelect(strSql);

            ss.QueryDBEvent += QueryDB;

            SearchSelect.Setting settings = new SearchSelect.Setting();

            ArrayList alFitlerFies = new ArrayList();
            alFitlerFies.Add("UserName");
            alFitlerFies.Add("Name");
            settings.AlFitlerFiels = alFitlerFies;
            settings.IsSingle = true;

            ss.Settings = settings;

            ss.ShowDialog();

            DataTable dtResult = ss.GetResult();

            if (dtResult.Rows.Count > 0)
            {
                textBox1.Text = dtResult.Rows[0]["UserName"].ToString();

            }
        }

        private DataTable QueryDB(string strSql)
        {
            return SqlHelper.Query(strSql).Tables[0];
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SelectUser();
            }
        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {

        }

        private void textBox1_Leave(object sender, EventArgs e)
        {

        }


    }
}
